package cn.t.converter.user;


import cn.t.dto.user.ParticipantAssignRoleRequest;
import cn.t.dto.user.ResourceInfoDTO;
import cn.t.dto.user.RoleAssignResourceRequest;
import cn.t.dto.user.RoleInfoDTO;
import cn.t.dto.user.UserInfoDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author l
 */
@Component
public class AssignRightsConverter {

    public List<RoleInfoDTO> toRoleList(ParticipantAssignRoleRequest request) {

        if (request.getRoleIds() == null || request.getRoleIds().isEmpty()) {
            return null;
        }

        return new HashSet<>(request.getRoleIds()).stream()
                .map(roleId -> {
                    RoleInfoDTO dto = new RoleInfoDTO();
                    dto.setId(roleId);
                    return dto;
                })
                .collect(Collectors.toList());
    }

    public UserInfoDTO toUserInfoDTO(ParticipantAssignRoleRequest request) {
        if (request.getUserId() == null || request.getUserId() < 1L) {
            return null;
        }

        UserInfoDTO dto = new UserInfoDTO();
        dto.setId(request.getUserId());

        return dto;
    }


    public List<ResourceInfoDTO> toResourceDtoList(RoleAssignResourceRequest request) {

        if (request.getResourceIdList() == null || request.getResourceIdList().isEmpty()) {
            return null;
        }

        return request.getResourceIdList().stream()
                .map(resourceId -> {
                    ResourceInfoDTO resourceInfoDTO = new ResourceInfoDTO();
                    resourceInfoDTO.setId(resourceId);
                    return resourceInfoDTO;
                })
                .collect(Collectors.toList());
    }

    public RoleInfoDTO toRoleInfoDTO(RoleAssignResourceRequest request) {
        if (request.getRoleId() == null || request.getRoleId() < 1L) {
            return null;
        }

        RoleInfoDTO dto = new RoleInfoDTO();
        dto.setId(request.getRoleId());

        if (StringUtils.isNotBlank(request.getRoleName())) {
            dto.setRoleName(request.getRoleName());
        }
        return dto;
    }

}